var registered; // To store registerd domains

$(function(){ 

    loadRegisteredDomains();
    
    loadData();
    
    // Check domain
    $('#btnCheck').click(function(){
        var domainName = $('#txtDomainName').val();
        var html = "";
        
        if(domainName == '') {
            alert('Domain name cannot be blank');
        } else {
            var checked = $('#extensions').find('.extention:checked').length;
            var checkedExts = (checked > 0) ? $('#extensions').find('.extention:checked') : $('#extensions').find('.extention');
            
            for(i = 0; i < checkedExts.length; i++) {
                var ext = $(checkedExts[i]);
                var domain = domainName + ext.attr('data-ext');
                
                if(isAvailable(domain)) {
                    html += '<li>' + domain + ' <a href="#" class="order" data-id="' + domain + '" data-name="' + domain + '" data-price="' + ext.attr('data-price') + '" data-min="' + ext.attr('data-minmonths') + '">Order</a></li>';
                } else {
                    html += '<li>' + domain + ' <span style="color: red;">is unavailable.</span></li>';
                }  
            }
        }
        
        $('#result').html(html);
        return false;
    });
    
    $('#result').on('click', '.order', function(){
        addToCart(this);
        return false;    
    });
});

function isAvailable(domain) {
    var result = true;
    
    $.each(registered, function(){
        if(domain == $(this).text()) {
            result = false;
            return false;    
        }                  
    });
    
    return result;    
}

function loadRegisteredDomains() {
    $.get('data/registered-domains.xml', function(data){
        registered = $(data).find('domain');   
    }, 'xml');    
}

function loadData() {
    $.get('data/domains.xml', function(data){
        var domains = $(data).find('domain');

        showExtentions(domains); 
        showPriceList(domains); 
    }, 'xml');
}

function showExtentions(domains) {
    
    var html = "";
    
    domains.each(function(){
        html += '<li><input type="checkbox" name="extention" class="extention" data-ext="'+ $(this).find('extension').text() + '" ' +
            'data-price="'+ $(this).find('pricePerMonth').text() +'" data-minMonths="'+ $(this).find('minMonths').text() +'"> '+ 
            $(this).find('extension').text() +'</li>';
    });

    $('#extensions').html(html);
}

function showPriceList(domains) {
    var html = "";

    domains.each(function(i){
        var trClass = (i % 2 != 0) ? 'alter' : '';
        html += '<tr class="'+ trClass +'">';
        html += '   <td>'+ $(this).find('extension').text() +'</td>';
        html += '   <td class="alignright">$'+ $(this).find('pricePerMonth').text() +'</td>';
        html += '   <td class="alignright">'+ $(this).find('minMonths').text() +'</td>';
        html += '   <td class="alignright">$'+ $(this).find('setupFee').text() +'</td>';
        html += '</tr>';    
    });

    $('#tblPriceList tbody').html(html); 
}